package com.parse.adapter;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.TextView;

import com.parse.api.WebComponet;
import com.parse.bean.ProduceResultDTO;
import com.parse.core.WebComponetImpl;
import com.parse.starter.ParseApplication;
import com.parse.starter.R;
import com.parse.starter.order.OrderActivity;
import com.parse.util.DownloadImage;

/**
 * 下訂單畫面
 * 
 * @author Andy
 * 
 */
public class OrderAdapter extends BaseAdapter {

	final static String OUT_TEXT = "訂購數量:%s\n單價:%s\n商品解說=%s\n";

	private OrderActivity activity;

	final private List<ProduceResultDTO> infos;

	final ParseApplication apply;

	public OrderAdapter(OrderActivity activity,
			final List<ProduceResultDTO> infos) {
		this.activity = activity;
		this.infos = infos;
		this.apply = (ParseApplication) activity.getApplication();

	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return this.infos.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return infos.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final View view = this.activity.getLayoutInflater().inflate(
				R.layout.order_view, null);

		final TextView name = (TextView) view.findViewById(R.id.name);
		final TextView price = (TextView) view.findViewById(R.id.price);
		// final TextView describe = (TextView)
		// view.findViewById(R.id.describe);
		final ImageView imageView = (ImageView) view.findViewById(R.id.pic);

		// imageView.setLayoutParams(new Gallery.LayoutParams(500, 500));

		final ProduceResultDTO info = infos.get(position);

		name.setText(info.getName());
		price.setText(String.format(OUT_TEXT, info.getNum(), info.getPrice(),
				info.getDescribe()));

		// describe.setText(info.getDescribe());

		if (this.apply.need2Download(info.getUrl())) {
			new DownloadImage(imageView, activity).execute(info);

		} else {

			imageView.setImageBitmap(apply.get(info.getObjectId()).getBitmap());
		}

		return view;
	}

	public class DownloadImage extends
			AsyncTask<ProduceResultDTO, Void, Bitmap> {
		private static final String TAG = "DownloadImage";
		private WebComponet webComponet = null;
		private ImageView image;
		private Activity ac;
		private String path;
		private ProduceResultDTO info = null;

		public DownloadImage(ImageView image, Context ct) {
			this.image = image;
			this.ac = (Activity) ct;
			this.webComponet = new WebComponetImpl();

		}

		@Override
		protected void onPreExecute() {
			// this.image.setImageResource(R.drawable.download);
			super.onPreExecute();
		}

		@Override
		protected Bitmap doInBackground(ProduceResultDTO... params) {

			info = params[0];
			this.path = (String) params[0].getUrl();
			try {

				return webComponet.getBitmapScale(path, 0.4f);

			} catch (OutOfMemoryError e) {

				return null;
			} catch (Exception e) {
				Log.v(TAG, "error=" + e);
				return null;
			}
		}

		@Override
		protected void onPostExecute(Bitmap result) {

			if (result == null) {

			} else {
				this.image.setImageBitmap(result);

				apply.putBitmap(info.getObjectId(), info.getUrl(), result);

				OrderAdapter.this.notifyDataSetChanged();
				// ac1.getListview().getAdapter().

			}

			super.onPostExecute(result);
		}

		@Override
		protected void onCancelled() {

			super.onCancelled();
		}

	}
}
